import 'package:flutter/material.dart';

/// 侧滑删除条目
/// 1. 创建列表项
/// 2. 使用Dismissible包裹列表条目
/// 3. 提供指示器

class SwipeDismissApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String appTitle = '';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
      home: SwipeDismissAppPage(title: appTitle),
    );
  }
}

class SwipeDismissAppPage extends StatefulWidget {
  final String title;

  SwipeDismissAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => SwipeDismissAppPageState();
}

class SwipeDismissAppPageState extends State<SwipeDismissAppPage> {
  final items = List<String>.generate(20, (i) => "Item ${i + 1}");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            final item = items[index];
            return Dismissible(
                key: Key(item),
                onDismissed: (direction) {
                  setState(() {
                    items.removeAt(index);
                  });
                  Scaffold.of(context)
                      .showSnackBar(SnackBar(content: Text("$item dismissed")));
                },
                // 设置滑动的背景颜色
                background: Container(
                  color: Colors.blue,
                ),
                child: ListTile(
                  title: Text('${items[index]}'),
                ));
          }),
    );
  }
}
